<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2017/12/24
 * Time: 15:51
 */

namespace controller;


use controller\base\Base;
use extend\wechat\pay\lib\WxPayConfig;
use model\ActionList;
use model\Egg;

class Order extends Base
{
    /**
     * @param int $id
     * @param int $num
     * @return string
     */
    public function create($id, $num)
    {
        $this->auth();
        $actionList = new ActionList();
        $actionList->where('id', $id);
        $actionListData = $actionList->row();
        if (empty($actionListData)) {
            return json(['state' => false, 'msg' => '网络错误001']);
        }

        $price    = $actionListData['egg_price'];
        $fee      = $num * $price;
        $order    = new \model\Order();
        $order_id = $order->insert([
            'tid'       => WxPayConfig::MCHID . date("YmdHis") . rand(100, 999),
            'action_id' => $id,
            'num'       => $num,
            'created'   => now(),
            'fee'       => $fee,
            'price'     => $price,
            'openid'    => session('openid'),
            'state'     => 1
        ]);
        if ($order_id) {
            return json(['state' => true, 'id' => $order_id]);
        } else {
            return json(['state' => false, 'msg' => '网络错误002']);
        }
    }
}